<template>
  <el-card>
    <header-search btn-name="添加商品" @Search="searchValue" @btn-click="$router.push('/goods/add')" />
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      border
    >
      <el-table-column
        type="index"
        label="#"
        width="40"
        align="center"
      />
      <el-table-column
        prop="goods_name"
        label="商品名称"
        width="560"
      />
      <el-table-column
        prop="goods_price"
        label="商品价格(元)"
      />
      <el-table-column
        prop="goods_weight"
        label="商品重量"
      />
      <el-table-column
        label="创建时间"
      >
        <template slot-scope="{row}">
          {{ (row.upd_time || row.add_time) | parseTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editGood(row)" />
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="delGood(row.goods_id)" />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="params.pagenum"
      :page-sizes="[5, 10, 15, 40]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
import HeaderSearch from '@/components/HeaderSearch/HeaderSearch'
import { getGoodsList } from '@/api/commodityManagement'
export default {
  name: 'Index',
  components: { HeaderSearch },
  data() {
    return {
      tableData: [],
      total: 0,
      params: {
        query: '',
        pagenum: 1,
        pagesize: 10
      }
    }
  },
  created() {
    this.loadGoodsList()
  },
  methods: {
    async loadGoodsList() {
      const res = await getGoodsList(this.params)
      this.tableData = res.goods
      this.total = res.total
    },
    searchValue(value) {
      this.params.pagenum = 1
      this.params.query = value
      this.loadGoodsList()
    },
    delGood(id) {

    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.loadGoodsList()
    },
    handleCurrentChange(val) {
      this.params.pagenum = val
      this.loadGoodsList()
    }
  }
}
</script>

<style scoped>
.el-input-group{
  width: 33%;
  margin-right: 30px;
}
</style>
